<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/7
 * Time: 1:51
 */
namespace App\Http\Controllers\Api\v1\User;
use App\Models\Api\User;
use App\Models\Api\MessageCenter;
use App\Models\Api\UserMsg;
use Cache;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\BaseController;


/**
 * @title 消息中心
 * @desc
 * @url
 * @version 2.0.0
 * Class Message
 * @package app\api\controller\v2
 */
class MessageController extends BaseController
{

    /**
     * @title
     * @method GET
     * @desc     消息列表
     * @return array
     */
    public function index(Request $request)
    {
        if(!$request->isMethod('get'))
        {
            return show(config('code.error'), '未知操作', [], 403 );
        }
        $type =$request->input('type');
        $page = $request->input('page',1);

        $uid = $request->uid;
        //$map['range'] =  array(['=',0],['=',$request->uid],'or');
        //$map['status'] =  array('!=',-1);
        //$map['type'] = $type;

        // 获取当前用户的所有消息
        $data = (new MessageCenter())->get_user_msg_id(['uid'=>$uid,'type'=>$type,'page'=>$page]);

        try
        {

            if ($data['data'])
            {
                foreach ($data['data'] as $k=>$v)
                {
                    //$data['data'][$k]['is_read'] = 0;
                    if ($v['cid']==0)
                    {
                        $data['data'][$k]['icon_url'] = '/wap/images/default.png';
                    }
                    elseif($v['cid']==1)
                    {
                        $data['data'][$k]['icon_url'] ='/wap/images/re.png';
                    }
                    elseif ($v['cid']==2)
                    {
                        $data['data'][$k]['icon_url'] = env('photo.dev').'test/fortune/luckystar.png';
                    }else{
                        $data['data'][$k]['icon_url'] = '/wap/images/default.png';
                    }
                };
            }
        }
        catch (\Exception $e)
        {
            return show(config('code.error'),$e->getMessage(),[],500);
        }
        return show(200,'成功',$data);
    }
    /**
     * @title
     * @method GET
     * @desc   获取不同类型的消息未读数量
     * @return array
     */
    public function is_read(Request $request)
    {
        if(!$request->isMethod('get'))
        {
            return show(config('code.error'), '未知操作', [], 403 );
        }

        $uid = $request->uid;

        $arr=[];

        $arr = [];

        //获取用户全部未读系统消息id
        $system_msg_id = (new MessageCenter())->getAllUserMessage(['uid' => $uid, 'type' => 1]);
        //获取用户全部未读服务消息id
        $server_msg_id2 =  (new MessageCenter())->getAllUserMessage(['uid' => $uid, 'type' => 2]);
        $server_msg_id3 =  (new MessageCenter())->getAllUserMessage(['uid' => $uid, 'type' => 3]);

        $arr['type1'] = count($system_msg_id);
        $arr['type2'] = count($server_msg_id2);
        $arr['type3'] = count($server_msg_id3);

        // 获取未阅读的回复量
        // $replyModel = new User();
        // $data =$replyModel->rMyComments(['user_id'=>$request->uid]);

        // if (!$data) $data=[];

        // $array =[];
        // foreach ($data as $k=>$v)
        // {
        //     if ($v['id'] && !$v['p_id'])
        //     {
        //         $array[]= [$v['id']];
        //     }
        // }

        // if ($data)
        // {
        //     $arr['type3']=count($array);
        // }
        // else
        // {
        //     $arr['type3']=0;
        // }

        return show(200,'成功',$arr);
    }


    /**
     * @title
     * @method POST
     * @desc   将未读状态改为已读
     * @return array
     */
    public function change_read(Request $request)
    {
        if(!$request->isMethod('put'))
        {
            return show(config('code.error'), '未知操作', [], 403 );
        }
        $id =intval($request->input('id'));
        $uid = $request->uid;

        $MsgModel = new MessageCenter();

        $msg = $MsgModel->get_msg_info(['ids' => [0 => $id]]);
        if ($msg)
        {
            //若当前消息为没有阅读状态则进行数据状态修改
            if ($msg[0]['is_read'] == 0)
            {
                try{
                    $MsgModel::where('id', $id)->update(['is_read' => 1]);
                    $MsgModel::rm_cache(['mid'=>$id]);
                    $MsgModel::rm_cache(['uid' => $uid, 'type' => 1, 'mid' => $id]);
                    $MsgModel::rm_cache(['uid' => $uid, 'type' => 2]);
                    $MsgModel::rm_cache(['uid' => $uid, 'type' => 3]);

                }
                catch (\Exception $e)
                {
                    return show(config('code.error'), '操作失败,请稍候再试');
                }
            };
        }

        return show(200, '提交成功', []);
    }
}
